<?php

namespace app\home\admin;
use app\admin\controller\Admin;
use app\home\model\User;
use think\Loader;
use think\Request;

class Users extends Admin
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
//        根据角色来获取对应的用户列表
        $data = $request->param();
        $validate = validate('Users');
        $res = $validate->scene('list')->check($data);
        if (!isset($data['role'])){
            $data['role'] = 1;
        }
        if (!$res){
            return $this->error($validate->getError());
        }
        //设置默认筛选条件
        $where['ischeck'] = ['in',[0,1]];
        $where['status'] = ['in',[0,1]];
        $where['role'] = $data['role'];

        //如果设置了删选是否审核
        if (isset($data['ischeck']) && $data['ischeck'] != 3){
            if ($data['ischeck'] == 1){
                $where['ischeck'] = 1;
            }else{
                $where['ischeck'] = 0;
            }

        }

        //是否设置了关键字
        if (isset($data['keyword']) && !empty($data['keyword'])){
            $where['nickname|phonenum'] = ['like',"%{$data['keyword']}%"];
        }
        //设置筛选是否禁用用户查询
        if (isset($data['status']) && $data['status'] != 3){
            $where['status'] = $data['status'];
        }
        if (!empty($data['create_time'])){
            $time = explode('~',$data['create_time']);
            $start = strtotime($time[0]);
            $end = strtotime($time[1]);
            $where['create_time'] = ['between',[$start,$end]];
        }
//        var_dump($where);die;
        isset($data['page']) ? $page = $data['page'] : $page = 1;
        $res = db('home_users')->where($where)->order('create_time desc')->paginate(7)->each(function ($item){
            $item['operation_time'] = date("Y-m-d H:i:s",$item['operation_time']);
            $item['create_time'] = date("Y-m-d H:i:s",$item['create_time']);
            return $item;
        });
        if ($res){
            $this->assign('userlist',$res);
            $this->assign('role',$data['role']);
        }else{
            return $this->success('缺少对应用户数据');
        }
        return $this->fetch();


    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function checkuser(Request $request)
    {
        $data = $request->param();
        $id = $data['id'];
        $info = db('home_users')->where('id',$id)->find();
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function changestatus(Request $request)
    {
        $data = $request->param();
        if (!empty($data['id'])){
            $id = $data['id'];
        }else{
            return $this->error('参数缺失');
        }
        if ($data['val'] == 0){
            $insert['status'] = 1;
        }else{
            $insert['status'] = 0;
        }
        $result = db('home_users')
            ->where('id',$id)
            ->update($insert);
        if ($result){
            return $this->success('操作成功');
        }else{
            return $this->error('操作失败');
        }
    }
}
